home *** CD-ROM | disk | FTP | other *** search
- unit SQLTraceU;
- {$ifdef Windows}
- 'This app requires the 32-bit BDE'
- {$endif}
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- BDE, Grids, DBGrids, DB, DBTables, StdCtrls, ExtCtrls, Buttons;
-
- type
- TTraceForm = class(TForm)
- lstTrace: TListBox;
- chkTableOpen: TCheckBox;
- DataSource1: TDataSource;
- Table1: TTable;
- Database1: TDatabase;
- DBGrid1: TDBGrid;
- TraceCategories: TGroupBox;
- CBPrepared: TCheckBox;
- CBExecuted: TCheckBox;
- CBInputParams: TCheckBox;
- CBFetchedData: TCheckBox;
- CBStatement: TCheckBox;
- CBConnect: TCheckBox;
- CBTransaction: TCheckBox;
- CBBlob: TCheckBox;
- CBMisc: TCheckBox;
- CBVendorErr: TCheckBox;
- CBVendor: TCheckBox;
- memTrace: TMemo;
- btnClear: TSpeedButton;
- Label1: TLabel;
- Label2: TLabel;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure chkTableOpenClick(Sender: TObject);
- procedure chkTraceCategoryClick(Sender: TObject);
- procedure lstTraceClick(Sender: TObject);
- procedure btnClearClick(Sender: TObject);
- private
- FTraceBuffer: PTraceDesc;
- FSQLTraceCallBack: TBDECallBack;
- function GetTraceFlags: TTraceFlags;
- function SQLTraceFunction(CBInfo: Pointer): CBRType;
- end;
-
- var
- TraceForm: TTraceForm;
-
- implementation
-
- {$R *.DFM}
-
- // Note that if Delphi 2 is running the component library will have opened
- // up the Database Explorer DLL (DBX.DLL) which will have opened up
- // SMCLIENT.DLL which is the important functionality of the SQL monitor.
- // If that DLL is opened but the SQL Monitor EXE isn't, then the Session
- // object will install its own SQL trace callback that will not work.
- // When it fails, it sets the Session's TraceFlags property [].
-
- //In short, don't expect this to work if executed when Delphi 2 is running
-
- procedure TTraceForm.FormCreate(Sender: TObject);
- begin
- //Give listbox a horizontal scroll bar
- SendMessage(lstTrace.Handle, lb_SetHorizontalExtent, 2000, 0);
- //Set session trace flags
- Session.TraceFlags := GetTraceFlags;
- //Initialise BDE before trying to install callback
- Session.Open;
- //Allocate callback descriptor
- GetMem(FTraceBuffer, SizeOf(TRACEDesc) + DBIMAXTRACELEN);
- //Install BDE callback
- FSQLTraceCallBack := TBDECallBack.Create(nil, nil, cbTRACE,
- FTraceBuffer, SizeOf(TRACEDesc) + DBIMAXTRACELEN,
- SQLTraceFunction, True);
- end;
-
- procedure TTraceForm.FormDestroy(Sender: TObject);
- begin
- //Uninstall BDE callback
- FSQLTraceCallBack.Free;
- FSQLTraceCallBack := nil;
- //Deallocate descriptor
- FreeMem(FTraceBuffer);
- FTraceBuffer := nil;
- end;
-
- procedure TTraceForm.chkTableOpenClick(Sender: TObject);
- begin
- Table1.Active := chkTableOpen.Checked
- end;
-
- function TTraceForm.GetTraceFlags: TTraceFlags;
- var
- I, TraceValue: Integer;
- begin
- TraceValue := 0;
- //Get Tag values of checked checkboxes
- for I := 0 to TraceCategories.ControlCount - 1 do
- if TraceCategories.Controls[I] is TCheckBox then
- if TCheckBox(TraceCategories.Controls[I]).Checked then
- Inc(TraceValue, TraceCategories.Controls[I].Tag);
- //Turn number into set
- Result := TTraceFlags(Word(TraceValue))
- end;
-
- function TTraceForm.SQLTraceFunction(CBInfo: Pointer): CBRType;
- begin
- //Set a result to avoid warning, even though it is ignored
- Result := cbrUSEDEF;
- lstTrace.Items.Add(StrPas(PTraceDesc(CBInfo).pszTrace));
- end;
-
- procedure TTraceForm.chkTraceCategoryClick(Sender: TObject);
- begin
- //Set session trace flags
- Session.TraceFlags := GetTraceFlags;
- end;
-
- procedure TTraceForm.lstTraceClick(Sender: TObject);
- begin
- memTrace.Text := lstTrace.Items[lstTrace.ItemIndex]
- end;
-
- procedure TTraceForm.btnClearClick(Sender: TObject);
- begin
- lstTrace.Clear
- end;
-
- end.
-